<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use api\utils\QuoteApiClient;

use herosphp\core\Loader;
use herosphp\utils\ArrayUtils;
use herosphp\utils\AjaxResult;

/**
 * 行情 action
 * @package admin\action
 * @author yu
 */
class QuoteAction extends CommonAction
{


    //行情管理->行情趋势
    public function trend(HttpRequest $request)
    {
        $search_arr = array(
            "brand_name" => $request->getParameter('brand_name', 'trim'),
            "addtime_s" => $request->getParameter('addtime_s', 'trim'),
            "addtime_e" => $request->getParameter('addtime_e', 'trim'),
        );
        if ($search_arr['addtime_s']) {

            $search_arr['addtime_s'] = date('Y-m-d', strtotime($search_arr['addtime_s']));
        } else {

            $search_arr['addtime_s'] = date('Y-m-d',strtotime('-7 day'));
        }

        if ($search_arr['addtime_e']) {

            $search_arr['addtime_e'] = date('Y-m-d', strtotime($search_arr['addtime_e']));
        } else {

            $search_arr['addtime_e'] = date('Y-m-d');
        }

        $url = "api/quote/showTrendData";
        $result = QuoteApiClient::send($url, array('pagesize' => 1000, 'page' => 1, 'search_arr' => $search_arr));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }


        $this->assign('trendItems', $arr['items']);
        $this->assign('trendChartData',$arr['trendChartData']);

        $this->assign('breadcrumb', array('行情管理', '行情趋势'));

        $this->setView('quote/trend');

    }

    //数据报表->品名数据详情
    public function user_product_statistics(HttpRequest $request)
    {


        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $search_arr = array();


        $url = "api/quote/searchByUserProductStatistics";
        $result = QuoteApiClient::send($url, array('pagesize' => $pagesize, 'page' => $page, 'search_arr' => $search_arr));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }


        $items = $arr['items'];

        if (!empty($items)) {
            $ids = array();
            foreach ($items as $val) {
                $ids[] = $val['saas_user_id'];
            }

            $ids_str = join("','", $ids);

            $userModel = Loader::model("user");

            $sql = "Select user.id , user_extends.wx_id,user.username From user Left Join user_extends On user.id=user_extends.user_id Where user.id In('$ids_str')";

            $users = $userModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

            $users = ArrayUtils::changeArrayKey($users, 'id');

            foreach ($items as $key => $value) {
                $items[$key]['username'] = $users[$value['saas_user_id']]['username'];
                $items[$key]['wx_id'] = $users[$value['saas_user_id']]['wx_id'];

            }

        }


        $this->generatePageMenu($arr['total']);
        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);
        $this->assign("title", "数据统计");
        $this->assign('breadcrumb', array('行情管理', '数据报表'));

        $this->setView('quote/user_product_statistics');

    }

    //数据报表->牌号数据详情
    public function user_brand_statistics(HttpRequest $request)
    {

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $search_arr = array();


        $url = "api/quote/searchByUserBrandStatistics";
        $result = QuoteApiClient::send($url, array('pagesize' => $pagesize, 'page' => $page, 'search_arr' => $search_arr));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }


        $items = $arr['items'];

        if (!empty($items)) {
            $ids = array();
            foreach ($items as $val) {
                $ids[] = $val['saas_user_id'];
            }

            $ids_str = join("','", $ids);

            $userModel = Loader::model("user");

            $sql = "Select user.id , user_extends.wx_id,user.username From user Left Join user_extends On user.id=user_extends.user_id Where user.id In('$ids_str')";

            $users = $userModel->getDB()->excute($sql)->fetchAll($fetch_style = 2);

            $users = ArrayUtils::changeArrayKey($users, 'id');

            foreach ($items as $key => $value) {
                $items[$key]['username'] = $users[$value['saas_user_id']]['username'];
                $items[$key]['wx_id'] = $users[$value['saas_user_id']]['wx_id'];

            }

        }


        $this->generatePageMenu($arr['total']);
        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);
        $this->assign("title", "数据统计");
        $this->assign('breadcrumb', array('行情管理', '数据报表'));

        $this->setView('quote/user_brand_statistics');

    }

    public function quotes(HttpRequest $request)
    {

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $search_arr = array();

        $product_name = $request->getParameter('tab');

        if ($product_name != '') {

            $search_arr = array('product_name' => $product_name);
        }

        $url = "api/quote/showQuoteList";
        $result = QuoteApiClient::send($url, array('pagesize' => $pagesize, 'page' => $page, 'search_arr' => $search_arr));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }
        $this->generatePageMenu($arr['total']);
        $this->assign('items', $arr['items']);
        $this->assign('search_arr', $search_arr);
        $this->assign('tab', $product_name);
        $this->assign('quote_domin',getConfig('quote_api_url'));
        $this->assign("title", "行情中心");
        $this->assign('breadcrumb', array('行情管理', '行情中心'));

        $this->setView('quote/quote_index');
    }

    public function quote_data(HttpRequest $request)
    {

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page = $page;
        $this->pagesize = $pagesize;

        $search_arr = "";

        $product_name = $request->getParameter('tab');
        $addtime_s = $request->getParameter('addtime_s', 'trim');
        $addtime_e = $request->getParameter('addtime_e', 'trim');

        if ($product_name != '') {

            $search_arr .= " and a.product_name = '{$product_name}'";
        }
        if( $addtime_s ){
            $search_arr .= " and a.addtime >= '".$addtime_s." 00:00:00'";
        }
        if( $addtime_e ){
            $search_arr .= " and a.addtime <= '".$addtime_e." 23:59:59'";
        }

        $url = "api/quote/showQuoteDataList";
        $result = QuoteApiClient::send($url, array('pagesize' => $pagesize, 'page' => $page, 'search_arr' => $search_arr));
        if ($result->isSucess()) {
            $arr = $result->getData();
        } else {
            $arr = array();
            error_log($result->getMessage());
        }
        $this->generatePageMenu($arr['total']);
        $this->assign('items', $arr['items']);
        $this->assign('search_arr', $search_arr);
        $this->assign('tab', $product_name);
        $this->assign('quote_domin',getConfig('quote_api_url'));
        $this->assign("title", "数据中心");
        $this->assign('breadcrumb', array('行情管理', '数据中心'));

        $this->setView('quote/quote_data');
    }

}
